
package me.zhengjie.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.domain.AppWork;
import me.zhengjie.service.AppWorkService;
import me.zhengjie.service.dto.AppWorkQueryCriteria;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author IQW
* @date 2022-07-03
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "巡护任务管理")
@RequestMapping("/api/appWork")
public class AppWorkController {

    private final AppWorkService appWorkService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('appWork:list')")
    public void exportAppWork(HttpServletResponse response, AppWorkQueryCriteria criteria) throws IOException {
        appWorkService.download(appWorkService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询巡护任务")
    @ApiOperation("查询巡护任务")
//    @PreAuthorize("@el.check('appWork:list')")
    public ResponseEntity<Object> queryAppWork(AppWorkQueryCriteria criteria,
                                               @PageableDefault(sort = {"startTime"}, direction = Sort.Direction.DESC) Pageable pageable){

        return new ResponseEntity<>(appWorkService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增巡护任务")
    @ApiOperation("新增巡护任务")
    @PreAuthorize("@el.check('appWork:add')")
    public ResponseEntity<Object> createAppWork(@Validated @RequestBody AppWork resources){
        return new ResponseEntity<>(appWorkService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改巡护任务")
    @ApiOperation("修改巡护任务")
    @PreAuthorize("@el.check('appWork:edit')")
    public ResponseEntity<Object> updateAppWork(@Validated @RequestBody AppWork resources){
        appWorkService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除巡护任务")
    @ApiOperation("删除巡护任务")
    @PreAuthorize("@el.check('appWork:del')")
    public ResponseEntity<Object> deleteAppWork(@RequestBody Integer[] ids) {
        appWorkService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}